<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="1000px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="120px">
          <el-form-item label="id" v-if="form.id!=null">
            <el-input v-model="form.id" style="width: 370px;" />
          </el-form-item>
        
          <el-form-item label="奖品名称">
            <el-input v-model="form.title" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="奖品类型">
              <el-radio v-model="form.type" :label="1">商品</el-radio>
              <el-radio v-model="form.type" :label="0" style="width: 200px;">积分</el-radio>
  
          </el-form-item>
          <el-form-item label="奖励积分" v-if="form.type==0">
            <el-input-number v-model="form.integral" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="奖励商品" v-if="form.type==1">
              <cgood v-model="form1.good" :disabled="true" ></cgood>
          </el-form-item>
          <el-form-item label="商品名称" v-if="form.type==1">
              <el-input v-model="form1.good.storeName" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="奖品数量">
            <el-input-number v-model="form.number" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="中奖概率">
            <el-input-number v-model="form.weight" style="width: 370px;" />
          </el-form-item>
  
  
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="id" label="id" />
        <el-table-column prop="prizeId" label="转盘主键id" />
        <el-table-column prop="title" label="奖品名称" />
        <el-table-column prop="type" label="奖品类型:0=积分,1=商品" />
        <el-table-column prop="integral" label="奖励积分(type=0时使用)" />
        <el-table-column prop="productId" label="奖励关联商品(type=1时使用)" />
        <el-table-column prop="number" label="奖品数量" />
        <el-table-column prop="weight" label="中奖概率" />
        <el-table-column prop="createTime" label="创建时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="updateTime" label="编辑时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.updateTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column v-permission="['admin','yxSystemPrizeItem:edit','yxSystemPrizeItem:del']" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import cgood from '@/views/components/good'
import crudYxSystemPrizeItem from '@/api/yxSystemPrizeItem'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import MaterialList from "@/components/material";

// crud交由presenter持有
const defaultCrud = CRUD({ title: '', url: 'api/yxSystemPrizeItem', sort: 'id,desc', crudMethod: { ...crudYxSystemPrizeItem }})
const defaultForm = { id: null, prizeId: null, title: null, type: null, integral: null, productId: null, number: null, weight: null, createTime: null, updateTime: null }
export default {
  name: 'YxSystemPrizeItem',
  components: { pagination, crudOperation, rrOperation, udOperation ,MaterialList,cgood},
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  data() {
    return {
      form1: {
        good:{
          productId: null,
          storeName: null,
          image: null,
          otPrice: null,
          price: null,
        }
      },
      
      permission: {
        add: ['admin', 'yxSystemPrizeItem:add'],
        edit: ['admin', 'yxSystemPrizeItem:edit'],
        del: ['admin', 'yxSystemPrizeItem:del']
      },
      rules: {
      }    }
  },
  watch: {
  },
  methods: {
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      return true
    }, // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
        if(this.form.productId==null){
          this.form.productId = this.form1.good.productId;
        }else{
          this.form1.good.productId = this.form.productId;
          this.form1.good.storeName = this.form.productId;
          this.form1.good.image = this.form.productId;
        }
          
        

    },
  }
}



</script>

<style scoped>
  .table-img {
    display: inline-block;
    text-align: center;
    background: #ccc;
    color: #fff;
    white-space: nowrap;
    position: relative;
    overflow: hidden;
    vertical-align: middle;
    width: 32px;
    height: 32px;
    line-height: 32px;
  }
</style>
